
window.onload=function (){
    var userInfo = getAuthority();
    var Authorization;//请求接口用
    //获取权限方法
    if (userInfo) {
        var access_token = localStorage.getExpire("access_token");
        var token_type = localStorage.getExpire("token_type");
        Authorization = token_type + " " + access_token;
        var userInfoData = JSON.parse(userInfo)
        document.getElementById('password').value = "";
    }else{
        location.href=webBaseUrl+'/page/login.html';
    }

    $("#cancel-btn").on("click",function(){
        location.href=webBaseUrl+'/page/login.html';
    });

    submitPassword();

    //登录
    function submitPassword() {
        layui.use(['layer', 'form'], function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;

            //自定义验证规则
            form.verify({
                password: [
                    /^[\S]{2,18}$/
                    ,'密码必须2到18位，且不能出现空格'
                ],
                confirmPass:function(value){
                    if($('input[name=password]').val() !== value)
                        return '两次密码输入不一致！';
                }
            });

            // 表单提交
            form.on('submit(login-submit)', function (obj) {
                var user = obj.field;
                var formData = new FormData();
                formData.append("password",user.password);
                axios({
                    method: 'put',
                    url:loginUrl.editUserPassword,
                    data:formData,
                    headers:{
                        'Authorization': Authorization}
                })
                    .then(function (response) {
                        if(response.status == 200&&response.data.message=="success"){
                            location.href=loginPageUrl;
                            localStorage.clear();
                            sessionStorage.clear();
                        }else {
                            console.log(response.data)
                        }
                    })
                    .catch(function (error) {
                        layui.use(['notice'], function(){
                            var notice = layui.notice;
                            // 提示框，1成功、2失败、3警告、4加载、5信息(蓝色图标)
                            notice.msg(JSON.parse(error.request.response).message, {icon: 2,timeout:2000});
                        });
                        console.log(error);
                    });
                return false;
            });

        });

    }
}



